package com.sfzd5.exam.helplibrary.recognition;

import org.bytedeco.javacpp.DoublePointer;
import org.bytedeco.javacpp.opencv_core;
import org.bytedeco.javacpp.opencv_core.Mat;
import org.bytedeco.javacpp.opencv_core.Point;

import static org.bytedeco.javacpp.opencv_core.CV_32FC1;
import static org.bytedeco.javacpp.opencv_core.NORM_MINMAX;
import static org.bytedeco.javacpp.opencv_imgproc.TM_SQDIFF;
import static org.bytedeco.javacpp.opencv_imgproc.matchTemplate;

public class TemplateMaching {

    public static Point process(Mat source, Mat dst){
        //创建于原图相同的大小，储存匹配度
        Mat result = new Mat(source.rows(),source.cols(), CV_32FC1);
        //调用模板匹配方法
        matchTemplate(source, dst, result, TM_SQDIFF);
        //规格化
        opencv_core.normalize(result, result, 0, 1, NORM_MINMAX, -1, null);
        //获得最可能点，MinMaxLocResult是其数据格式，包括了最大、最小点的位置x、y
        //MinMaxLocResult mlr = opencv_core.minMaxLoc(result);
        DoublePointer minVal= new DoublePointer();
        DoublePointer maxVal= new DoublePointer();
        Point min = new Point();
        Point max = new Point();
        opencv_core.minMaxLoc(result, minVal, maxVal, min, max, null);
        return min;
    }
}
